package com.douqu.game.core.config;

import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.e.E_FlashSaleType;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.entity.ext.data.boon.FlashSaleRecord;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.util.CoreUtils;
import com.douqu.game.core.util.LoadUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wangzhenfei
 * 2017-12-04 14:32
 * 充值配置
 */
public class FlashSaleConfig extends GameObject {
    public int groupId;
    public int index;
    public int type;
    public int condition;
    public int showTime;
    public int[] openTime;
    public int times;
    public int[] level;
    public int rechargeId;
    public GoodsData[] cost;
    public GoodsData[] goods;

    /**
     * 初始化限时促销基础数据
     *
     * @param flashSaleRecordMap
     * @param \isInit是否是初始化,false=重置
     */
    public static void initOrReset(Map<Integer, List<FlashSaleRecord>> flashSaleRecordMap, boolean isInit) {
        if (!isInit) {
            for (E_FlashSaleType flashSaleType : E_FlashSaleType.values()) {
                if (flashSaleType != E_FlashSaleType.PASS_INSTANCE) {
                    flashSaleRecordMap.remove(flashSaleType.getCode());
                }
            }
        }

        Map<Integer, FlashSaleConfig> map = DataFactory.getInstance().getDataMap(DataFactory.FLASH_SALE_KEY);
        for (Map.Entry<Integer, FlashSaleConfig> e : map.entrySet()) {
            if (!isInit && e.getValue().type == E_FlashSaleType.PASS_INSTANCE.getCode()) {
                continue;
            }

            if (e.getValue().index == 1) {
                List<FlashSaleRecord> flashSaleRecords = flashSaleRecordMap.get(e.getValue().type);
                if (flashSaleRecords == null) {
                    flashSaleRecords = new ArrayList<>();
                    flashSaleRecordMap.put(e.getValue().type, flashSaleRecords);
                }
                flashSaleRecords.add(new FlashSaleRecord(e.getValue().id));
            }
        }
    }

    /**
     * 时间到，或者完成查找下一次配置
     *
     * @param fsr
     */
    public static void findNext(FlashSaleRecord fsr) {
        // 次数-1
        fsr.times -= 1;

        if (fsr.times == 0) {
            FlashSaleConfig flashSaleConfig = DataFactory.getInstance().getGameObject(DataFactory.FLASH_SALE_KEY, fsr.id);
            if (flashSaleConfig == null) {
                // 配置不存在，结束此类任务
                fsr.finish();
                return;
            }

            boolean hasNext = false;
            Map<Integer, FlashSaleConfig> map = DataFactory.getInstance().getDataMap(DataFactory.FLASH_SALE_KEY);
            for (Map.Entry<Integer, FlashSaleConfig> e : map.entrySet()) {
                if (e.getValue().index == flashSaleConfig.index + 1 && e.getValue().groupId == flashSaleConfig.groupId) {
                    // 找得到下一个
                    fsr.next(e.getValue().id);
                    hasNext = true;
                    break;
                }
            }
            if (!hasNext) {
                // 找不到下一个，结束此类任务
                fsr.finish();
            }
        } else {
            // 此类活动次数未完成，清除状态可以继续触发
            fsr.clear();
        }
    }

    @Override
    public boolean check() {
        return true;
    }

    @Override
    public boolean setVariable(String key, String value) {
        if ("level".equals(key)) {
            level = LoadUtils.loadToArray(key, value);
            return true;
        } else if ("openTime".equals(key)) {
            openTime = LoadUtils.loadToArray(key, value);
            return true;
        } else if ("cost".equals(key)) {
            if(CoreUtils.isErrorValue(value))
                cost = new GoodsData[0];
            else
                cost = LoadUtils.loadGoodDataToArray(key, value);
            return true;
        } else if ("goods".equals(key)) {
            if(CoreUtils.isErrorValue(value))
                goods = new GoodsData[0];
            else
                goods = LoadUtils.loadGoodDataToArray(key, value);
            return true;
        }else
            return super.setVariable(key, value);
    }
}
